package cn.fuxi2022.accounts.menu.controller;


import cn.fuxi2022.accounts.menu.service.AccountsMenuService;
import cn.fuxi2022.accounts.menu.service.WeChatMenuService;
import cn.fuxi2022.core.exception.SmartGroupException;
import cn.fuxi2022.model.empty.accounts.AccountsMenu;
import cn.fuxi2022.model.resp.R;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * <p>
 * 微信公众号菜单表 前端控制器
 * </p>
 *
 * @author Fuxi
 * @since 2022-10-18 14:57:39
 */
@RestController
@RequestMapping("/admin/menu")
public class AccountsMenuController {
    @Autowired
    private AccountsMenuService accountsMenuService;

    @Autowired
    private WeChatMenuService weChatMenuService;

    private void syncWeChatMenu(){
        weChatMenuService.removeMenu();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        weChatMenuService.syncMenu();
    }

    /**
     * 列表
     */
    @PostMapping("/list")
    public R list() {
        return R.ok().data(accountsMenuService.getAllList());
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") String id) {
        AccountsMenu accountsMenu = accountsMenuService.getById(id);

        return R.ok().data("accountsMenu", accountsMenu);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody @Validated(AccountsMenu.Save.class) AccountsMenu accountsMenu) {
        if ("0".equals(accountsMenu.getParentId())) {
            // 判断当前的一级菜单是否大于3
            LambdaQueryWrapper<AccountsMenu> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(AccountsMenu::getParentId, "0");
            if (accountsMenuService.count(wrapper) >= 3) {
                throw new SmartGroupException("一级菜单数量过多");
            }
        } else {
            // 判断该父分类是否存在
            LambdaQueryWrapper<AccountsMenu> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(AccountsMenu::getId, accountsMenu.getParentId());
            if (accountsMenuService.count(wrapper) != 1) {
                throw new SmartGroupException("父分类非法");
            }

            // 判断当前的一级菜单是否大于5
            LambdaQueryWrapper<AccountsMenu> accountsMenuWrapper = new LambdaQueryWrapper<>();
            accountsMenuWrapper.eq(AccountsMenu::getParentId, accountsMenu.getParentId());
            if (accountsMenuService.count(accountsMenuWrapper) >= 5) {
                throw new SmartGroupException("二级菜单数量过多");
            }
        }

        accountsMenuService.save(accountsMenu);
        syncWeChatMenu();
        return R.ok("保存菜单成功");
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public R update(@RequestBody @Validated(AccountsMenu.Update.class) AccountsMenu accountsMenu) {
        accountsMenuService.updateById(accountsMenu);
        syncWeChatMenu();
        return R.ok("修改菜单成功");
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody String[] ids) {

        LambdaQueryWrapper<AccountsMenu> wrapper = new LambdaQueryWrapper<>();
        for (int i = 0; i < ids.length; i++) {
            wrapper.eq(AccountsMenu::getParentId, ids[i]);
            if (i < ids.length - 1) {
                wrapper.or();
            }
        }

        accountsMenuService.remove(wrapper);
        accountsMenuService.removeByIds(Arrays.asList(ids));
        syncWeChatMenu();

        return R.ok("删除菜单成功");
    }
}
